unit libGetRunningNoF;

interface

uses
  SysUtils, Variants, uniGUIDialogs, libDialog_CustomF, uniGUIBaseClasses,
  uniGUIClasses, uniImageList, MainModule, uniDBGrid, DB, Classes, Controls,
  Forms, ADODB, uniBasicGrid, uniToolBar, ActnList, ImgList;

type
  TfmGetRunningNo = class(TfmDialog_Custom)
    srcDetails: TDataSource;
    grdGetRunningNo: TUniDBGrid;
    dsMultiGenNo: TADODataSet;
    dsMultiGenNofldKeyFieldNo: TStringField;
    dsMultiGenNofldKeyFieldNoFormat: TStringField;
    dsMultiGenNofldAutoGenNoDate: TDateTimeField;
    dsMultiGenNofldSeqNo: TIntegerField;
    dsMultiGenNofldDefault: TBooleanField;
    cmdDoGetGenNo: TADOCommand;
    cmdUpdateGenNo: TADOCommand;
    procedure UniFormCreate(Sender: TObject);
  private
    _sGenNoType: string;
    _iAutoGenNoSeqNo: Integer;
    _dGenNoDate: TDateTime;
    _bMulitGenNo: boolean;
    _bShowGenNo: boolean;
    _Result: string;
    procedure _CallBack(Sender: TComponent; AResult: Integer);
  public
    function sGetRunningNo(sFunctionCode: string; var sGenNoType: string; var iAutoGenNoSeqNo: integer; var dGenNoDate: TDateTime; var bShowGenNo, bMulitGenNo: boolean): string;
    procedure doSaveRunningNo(var sKeyValue: string; sFunctionCode: string; bMulitGenNo: boolean; iAutoGenNoSeqNo: integer);
  end;

function fmGetRunningNo: TfmGetRunningNo;

var
  FfmGetRunningNo: TfmGetRunningNo;

implementation

uses
  libParamsU, libPublicInterfaceU, uniGUIApplication;

{$R *.dfm}

function fmGetRunningNo: TfmGetRunningNo;
begin
  if FfmGetRunningNo = nil then
    FfmGetRunningNo := TfmGetRunningNo.Create(UniApplication);
  Result := FfmGetRunningNo;
end;

procedure TfmGetRunningNo.doSaveRunningNo(var sKeyValue: string; sFunctionCode: string; bMulitGenNo: boolean; iAutoGenNoSeqNo: integer);
//var
//  aRecordSet: _RecordSet;
begin
  cmdUpdateGenNo.Parameters.ParamByName('@FunctionCode').Value := sFunctionCode;
  cmdUpdateGenNo.Parameters.ParamByName('@dDate').Value := sDate;
  cmdUpdateGenNo.Parameters.ParamByName('@bMultiGenNo').Value := bMulitGenNo;
  cmdUpdateGenNo.Parameters.ParamByName('@iSeqNo').Value := iAutoGenNoSeqNo;
  cmdUpdateGenNo.Execute;
  sKeyValue := cmdUpdateGenNo.Parameters.ParamByName('@KeyFieldNo').Value;
end;

function TfmGetRunningNo.sGetRunningNo(sFunctionCode: string; var sGenNoType: string; var iAutoGenNoSeqNo: integer; var dGenNoDate: TDateTime; var bShowGenNo, bMulitGenNo: boolean): string;
//var
//  RecrodSet: _Recordset;
begin
  Result := '';
  cmdDoGetGenNo.Parameters.ParamByName('@FunctionCode').Value := sFunctionCode;
  cmdDoGetGenNo.Parameters.ParamByName('@dDate').Value := sDate;
  cmdDoGetGenNo.Execute;
  if cmdDoGetGenNo.Parameters.ParamByName('@bMultiGenNo').Value then
  begin
    dsMultiGenNo.Open;
    if bShowGenNo then
    begin
      srcDetails.DataSet := dsMultiGenNo;
      ShowModal(_CallBack);
//      sGenNoType := _sGenNoType;
//      iAutoGenNoSeqNo := _iAutoGenNoSeqNo;
//      dGenNoDate := _dGenNoDate;
//      bMulitGenNo := _bMulitGenNo;
//      Result := _Result;
    end
    else
    begin
      dsMultiGenNo.Locate('fldSeqNo', IntToStr(iAutoGenNoSeqNo), []);
      Result := dsMultiGenNo['fldKeyFieldNo'];
    end;
  end
  else if cmdDoGetGenNo.Parameters.ParamByName('@KeyFieldNo').Value <> '' then
  begin
    sGenNoType := '';
    iAutoGenNoSeqNo := 0;
    bMulitGenNo := False;
    Result := cmdDoGetGenNo.Parameters.ParamByName('@KeyFieldNo').Value;
  end;
end;

procedure TfmGetRunningNo.UniFormCreate(Sender: TObject);
begin
  inherited;
  Caption := UniMainModule.sGetLanguage('GetRunningNoDialog');
  srcDetails.DataSet.Locate('fldDefault', 1, []);
end;

procedure TfmGetRunningNo._CallBack(Sender: TComponent; AResult: Integer);
begin
  case AResult of
    mrOk:
      begin
        _sGenNoType := srcDetails.DataSet['fldKeyFieldNoFormat'];
        _iAutoGenNoSeqNo := srcDetails.DataSet['fldSeqNo'];
        _dGenNoDate := srcDetails.DataSet['fldAutoGenNoDate'];
        _bMulitGenNo := True;
        _Result := srcDetails.DataSet['fldKeyFieldNo'];
      end;
    mrCancel:
      ;
  end;
end;

end.

